package com.moli.lumpSugar.user.controller;



import com.moli.lumpSugar.common.domain.vo.response.ApiResult;
import com.moli.lumpSugar.common.utils.AssertUtil;
import com.moli.lumpSugar.common.utils.RequestHolder;
import com.moli.lumpSugar.user.domain.dto.ItemInfoDTO;
import com.moli.lumpSugar.user.domain.dto.SummeryInfoDTO;
import com.moli.lumpSugar.user.domain.entity.User;
import com.moli.lumpSugar.user.domain.enums.RoleEnum;
import com.moli.lumpSugar.user.domain.vo.req.user.BlackReq;
import com.moli.lumpSugar.user.domain.vo.req.user.ItemInfoReq;
import com.moli.lumpSugar.user.domain.vo.req.user.ModifyNameReq;
import com.moli.lumpSugar.user.domain.vo.req.user.SummeryInfoReq;
import com.moli.lumpSugar.user.domain.vo.req.user.WaringBadgeReq;
import com.moli.lumpSugar.user.domain.vo.resp.BadgeResp;
import com.moli.lumpSugar.user.domain.vo.resp.UserInfoResp;
import com.moli.lumpSugar.user.service.IRoleService;
import com.moli.lumpSugar.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author zhangjt
 * @since 2024-08-16
 */
@RestController
@RequestMapping("/sapi/user")
@Api(value = "用户相关信息接口")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private IRoleService iRoleService;

    @GetMapping("/getUserInfo")
    @ApiOperation(value = "获取用户信息")
//    @FrequencyControl(time = 60, count = 20, target = FrequencyControl.Target.UID)
    public ApiResult<UserInfoResp> getUserInfo() {
        return ApiResult.success(userService.queryUserInfo(RequestHolder.get().getUid()));
    }

    @PutMapping("/name")
    @ApiOperation(value = "修改用户名称")
    public ApiResult<Void> updateUserName(@Valid @RequestBody ModifyNameReq modifyNameReq) {
        userService.updateUserName(RequestHolder.get().getUid(),modifyNameReq.getName());
        return ApiResult.success();
    }

    @GetMapping("/getBadges")
    @ApiOperation(value = "获取徽章信息")
    public ApiResult<List<BadgeResp>> getBadges() {
        return ApiResult.success(userService.getBadges(RequestHolder.get().getUid()));
    }

    @PutMapping("/waringBadge")
    @ApiOperation(value = "佩戴徽章")
    public ApiResult<Void> waringBadge(@Valid @RequestBody WaringBadgeReq req) {
        userService.waringBadge(RequestHolder.get().getUid(),req.getBadgeId());
        return ApiResult.success();
    }

    @PutMapping("/black")
    @ApiOperation(value = "拉黑用户")
    public ApiResult<Void> black(@Valid @RequestBody BlackReq req) {
        Long uid = RequestHolder.get().getUid();
        boolean hasPower = iRoleService.hasPower(uid, RoleEnum.ADMIN);
        AssertUtil.isTrue(hasPower, "方糖管理员没权限");
        userService.black(req);
        return ApiResult.success();
    }

    @PostMapping("/public/summary/userInfo/batch")
    @ApiOperation("用户聚合信息-返回的代表需要刷新的")
    public ApiResult<List<SummeryInfoDTO>> getSummeryUserInfo(@Valid @RequestBody SummeryInfoReq req) {
        return ApiResult.success(userService.getSummeryUserInfo(req));
    }

    @PostMapping("/public/badges/batch")
    @ApiOperation("徽章聚合信息-返回的代表需要刷新的")
    public ApiResult<List<ItemInfoDTO>> getItemInfo(@Valid @RequestBody ItemInfoReq req) {
        return ApiResult.success(userService.getItemInfo(req));
    }

    @GetMapping("/getUser")
    @ApiOperation(value = "获取用户信息")
//    @FrequencyControl(time = 60, count = 20, target = FrequencyControl.Target.UID)
    public User getUser(Long uid) {
        return userService.getUser(uid);
    }

    @GetMapping("/getUserByOpenId")
    @ApiOperation(value = "根据OpenId获取用户信息")
//    @FrequencyControl(time = 60, count = 20, target = FrequencyControl.Target.UID)
    public User getUserByOpenId(String openId) {
        return userService.getUserByOpenId(openId);
    }

    @GetMapping("/getUserListByIds")
    @ApiOperation(value = "根据用户id批量获取用户信息")
    public List<User> getUserListByIds(List<Long> ids) {
        return userService.getUserListByIds(ids);
    }

    @PostMapping("/updateUser")
    @ApiOperation(value = "更新用户")
    public Boolean updateUser(@Valid @RequestBody User user) {
        return userService.updateUserById(user);
    }

}

